<?php

/**
 * This is the model class for table "education".
 *
 * The followings are the available columns in table 'education':
 * @property integer $id
 * @property string $code_id
 * @property string $school_name
 * @property string $major
 * @property string $start_year
 * @property string $end_year
 * @property integer $status
 */
class Education extends EducationBase
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Education the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, code_id, status', 'numerical', 'integerOnly'=>true),
			array('school_name', 'length', 'max'=>128),
			array('major', 'length', 'max'=>255),
			array('start_year, end_year', 'length', 'max'=>7),
			array('start_year, end_year', 'match', 'pattern' => '/^(0[1-9]|1[0-2])\/([12]\d{3})$/', 'message' => 'Thời gian không đúng định dạng (tháng/năm).'),
			array('end_year', 'checkEndYear'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, code_id, school_name, major, start_year, end_year, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'membercode' => array(self::BELONGS_TO, 'MemberCode', 'code_id'),
			'semester' => array(self::HAS_MANY, 'Semester', 'education_id'),
		);
	}

	public function checkEndYear($attribute,$params) {
		if(!$this->hasErrors() && $this->end_year != '' && $this->start_year != '') {
			$arrStartYear	= explode('/', $this->start_year);
			$arrEndYear		= explode('/', $this->end_year);
			$startDate		= date("Ym", strtotime($arrStartYear[0] . "/01/". $arrStartYear[1] . " 00:00:00"));
			$endDate		= date("Ym", strtotime($arrEndYear[0] . "/01/". $arrEndYear[1] . " 00:00:00"));

			if ($startDate >= $endDate)
				$this->addError("end_year", "Thời gian tốt nghiệp không hợp lí.");
		}
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID Education',
			'code_id' => 'Code',
			'school_name' => 'Tên trường',
			'major' => 'Chuyên ngành',
			'start_year' => 'Năm bắt đầu học',
			'end_year' => 'Năm dự kiến tốt nghiệp',
			'status' => 'Trạng thái',
		);
	}

 	public function getEducation($code) {
		$criteria=new CDbCriteria;

		$criteria->compare('status', 1);
		$criteria->compare('code_id',$code);

		return $this->find($criteria);
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('code_id',$this->code_id);
		$criteria->compare('school_name',$this->school_name,true);
		$criteria->compare('major',$this->major,true);
		$criteria->compare('start_year',$this->start_year,true);
		$criteria->compare('end_year',$this->end_year,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}